package com.ddwl.user.dao;

import com.ddwl.common.dao.TkDao;
import com.ddwl.schema.bo.user.LowerUserDistQueryBo;
import com.ddwl.schema.util.DateUtil;
import com.ddwl.user.dao.dto.DistUserListQueryDto;
import com.ddwl.user.dao.dto.UserDistInfoDto;
import com.ddwl.user.dao.dto.UserInviteCountDto;
import com.ddwl.user.dao.dto.UserInviteCountQueryDto;
import com.ddwl.user.dao.mapper.UserDistStatisticalMapper;
import com.ddwl.user.dao.model.UserDistStatistical;
import org.springframework.stereotype.Repository;

import java.time.Instant;
import java.util.List;

/**
 * 类UserDistStatisticalDao.java的实现描述：用户分销统计数据操作
 *
 * @author zach
 * @Date :2020-08-31 14:47.
 */
@Repository
public class UserDistStatisticalDao extends TkDao<UserDistStatisticalMapper, UserDistStatistical> {


    /**
     * 查询下线
     */
    public UserDistStatistical getUserDist(String userId){
        UserDistStatistical query = new UserDistStatistical();
        query.setUserId(userId);
        UserDistStatistical userDistStatistical = selectOne(query);
        if(userDistStatistical ==null){
            userDistStatistical = initUserDistStatistical(userId);
        }
        return  selectOne(query);
    }


    /**
     * 查询下线
     */
    public void UserQueryLower(String userId, LowerUserDistQueryBo bo){
        DistUserListQueryDto dto = new DistUserListQueryDto();
        baseMapper.queryDistUserList(dto);
    }

    /**
     * 查询分销用户信息
     * @param dto
     * @return
     */
    public List<UserDistInfoDto> queryDistUserList(DistUserListQueryDto dto){
        return baseMapper.queryDistUserList(dto);
    }

    /**
     * 查询分销用户当日邀请信息
     * @param userId
     * @return
     */
    public UserInviteCountDto queryUserInviteCount(String userId,String topAgent){
        UserInviteCountQueryDto dto  = new UserInviteCountQueryDto();
        if(userId.equals(topAgent)){
            dto.setTopAgent(topAgent);
        }else{
            dto.setUserId(userId);
        }
        dto.setEndTime(Instant.now());
        dto.setStartTime(DateUtil.getTimesMorning());
        return baseMapper.queryUserInviteCount(dto);
    }
    /**
     * 查询分销用户累计邀请信息
     * @param bo
     * @return
     */
    public UserInviteCountDto queryUserInviteCountNew(UserInviteCountQueryDto bo){
        return baseMapper.queryUserInviteCountNew(bo);
    }

    /**
     * 查询所有用户累计邀请信息(订正数据用)
     * @param bo
     * @return
     */
    public List<UserInviteCountDto> queryUserInviteCountAll(UserInviteCountQueryDto bo){
        return baseMapper.queryUserInviteCountAll(bo);
    }


    /**
     * 查询用户分销统计数据
     * @param userId
     * @return
     */
    public UserDistStatistical getUserDistStatistical(String userId) {
        UserDistStatistical query = new UserDistStatistical();
        query.setUserId(userId);
        UserDistStatistical userDistStatistical = selectOne(query);
        if(userDistStatistical==null){
            userDistStatistical = initUserDistStatistical(userId);
        }
        return userDistStatistical;
    }


    /**
     * 设置用户分销统计信息
     * @param userDistStatistical
     * @return
     */
    public UserDistStatistical setUserDistStatistical(UserDistStatistical userDistStatistical){
        updateByPrimaryKeySelective(userDistStatistical);
        return userDistStatistical;
    }


    /**
     * 初始化用户分销统计数据
     * @param userId
     */
    public UserDistStatistical initUserDistStatistical(String userId){
        UserDistStatistical userDistStatistical =  UserDistStatistical.initUserDistStatistical(userId);
        insert(userDistStatistical);
        return userDistStatistical;
    }

    public void addPopularizeNum(UserDistStatistical user) {
        baseMapper.addPopularizeNum(user);
    }

    public void updateUserDistStatistical(UserDistStatistical entity) {
        baseMapper.updateUserDistStatistical(entity);
    }

    public void subtractPopularizeNum(UserDistStatistical entity) {
        baseMapper.subtractPopularizeNum(entity);
    }
}
